﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Web.Security;
using System.Web.UI;
using System.Xml;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using DAL.DateSet;
using BLL.Wrapper;
using DAL;
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            /*XmlTextWriter writer = new
            XmlTextWriter(Server.MapPath("userInfo.xml"), Encoding.UTF8);

            // start writing!
            writer.WriteStartDocument();
            writer.WriteStartElement("userInfo");

            // Creating the ＜browserInfo＞ element
            writer.WriteStartElement("browserInfo");

            if (Request.UrlReferrer == null)
                writer.WriteElementString("urlReferrer", "none");
            else
                writer.WriteElementString("urlReferrer",
                Request.UrlReferrer.PathAndQuery);

            writer.WriteElementString("userAgent", Request.UserAgent);
            writer.WriteElementString("userLanguages",
            String.Join(", ", Request.UserLanguages));
            writer.WriteEndElement();

            // Creating the ＜visitInfo＞ element
            writer.WriteStartElement("visitInfo");
            writer.WriteAttributeString("timeVisited", DateTime.Now.ToString());
            writer.WriteElementString("ip", Request.UserHostAddress);
            writer.WriteElementString("rawUrl", Request.RawUrl);
            writer.WriteEndElement();

            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();*/
            //TypeWrapper wrapper = new TypeWrapper();

            HouseDiyHelper helper = HouseDiyHelper.GetInstance();
            string procname = "pagedivide";
            string sql = "select * from Type";
            // 创建参数
            IDataParameter[] parameters = {
                new SqlParameter("@SQL", SqlDbType.VarChar,400) ,
                new SqlParameter("@Page", SqlDbType.Int,4) ,
                new SqlParameter("@RecsPerPage", SqlDbType.Int,4) ,
                new SqlParameter("@ID", SqlDbType.VarChar,15) ,
                new SqlParameter("@Sort", SqlDbType.VarChar,15) ,
            };
            // 设置参数类型
            parameters[0].Value = sql;  
            parameters[1].Value = "5"; //页码..
            parameters[2].Value = "5";  //每页的行数
            parameters[3].Value = "typeID"; //关键字段
            parameters[4].Value = "asc";   //排序方法
            // 添加参数
            DataTable table = new DataTable();
            table = helper.getResultBySqlAndProcedure(procname, sql, parameters);
            grdvw.DataSource = table;
            grdvw.DataBind();
        }

        protected void button1Click(object sender, EventArgs e)
        {
            
            XmlDocument xmldoc;
            XmlNode xmlnode;
            XmlElement xmlelem;
            XmlElement xmlelem2;
            XmlText xmltext;
            xmldoc = new XmlDocument();

            //加入XML的声明段落
            xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
            xmldoc.AppendChild(xmlnode);

            //加入一个根元素    
            xmlelem = xmldoc.CreateElement("", "ROOT", "");
            xmltext = xmldoc.CreateTextNode("Root Text");
            xmlelem.AppendChild(xmltext);
            xmldoc.AppendChild(xmlelem);

            //加入另外一个元素
            xmlelem2 = xmldoc.CreateElement("SampleElement");
            xmlelem2 = xmldoc.CreateElement("", "SampleElement", "");
            xmltext = xmldoc.CreateTextNode("The text of the sample element");
            xmlelem2.AppendChild(xmltext);
            xmldoc.ChildNodes.Item(1).AppendChild(xmlelem2);

            //保存创建好的XML文档
          
            xmldoc.Save("D:\\data.xml");
            Console.ReadLine();
            XmlDocument list = getAllXmlItem();
            saveAs(list, "node");
            
        }

        public XmlDocument getAllXmlItem()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(System.Web.HttpContext.Current.Server.MapPath("typeList.xml"));
            return xmlDoc;
        }

        public void saveAs(XmlDocument doc, string rootname)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(System.Web.HttpContext.Current.Server.MapPath("typeListnew.xml"));
            XmlNode node = doc.SelectSingleNode(rootname);
            XmlNode root = xmlDoc.SelectSingleNode("node");
            XmlNode nd = xmlDoc.ImportNode(node, true);
            root.AppendChild(nd);
            xmlDoc.Save(System.Web.HttpContext.Current.Server.MapPath("typeListnew.xml"));
        }

        protected void indexchanging(object sender, GridViewPageEventArgs e)
        {
            grdvw.PageIndex = e.NewPageIndex;

        }
        protected void Page_indexchenged(object sender, EventArgs e)
        {
            grdvw.DataBind();
        }
    }
